package com.corder.display.common;

import com.corder.display.controller.UserController;
import com.corder.display.dao.Advertisement;
import com.corder.display.dao.CreateKey;
import com.corder.display.dao.Menu;
import com.corder.display.dao.Product;
import com.corder.display.dao.User;
import com.corder.display.util.Constant;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;

public class BaseConfig extends JFinalConfig {

	private static boolean isLocal = isLocal();
	private static String url = null;
	private static String username = null;
	private static String password = null;
	
	static {
		if (isLocal) {
			url = Constant.LOCAL_MYSQL_URL;
			username = Constant.LOCAL_MYSQL_USER;
			password = Constant.LOCAL_MYSQL_PASSWORD;
		} else {
			url = Constant.WEB_MYSQL_URL;
			username = Constant.LOCAL_MYSQL_USER;
			password = Constant.LOCAL_MYSQL_PASSWORD;
		}
	}
	
	@Override
	public void configConstant(Constants me) {
		loadPropertyFile("base.properties");
		me.setBaseViewPath("/WEB-INF/view");
		me.setEncoding("utf-8");
		if(isLocal) {
			me.setDevMode(true);
		}
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/back", UserController.class);
	}

	@Override
	public void configPlugin(Plugins me) {
		C3p0Plugin c3p0Config = new C3p0Plugin(url, username, password);
		me.add(c3p0Config);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Config);
		if (isLocal) {
			arp.setShowSql(true);
		}
		arp.addMapping("advertisement", Advertisement.class);
		arp.addMapping("menu", Menu.class);
		arp.addMapping("product", Product.class);
		arp.addMapping("user", User.class);
		arp.addMapping("createKey", CreateKey.class);
		me.add(arp);
	}

	@Override
	public void configInterceptor(Interceptors me) {

	}

	@Override
	public void configHandler(Handlers me) {

	}

	private static boolean isLocal(){
		boolean flag = false;
		String system = System.getProperty("os.name");
		if(system.startsWith("Windows")){
			flag = true;
		}
		return flag;
	}
}
